﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;

// UI管理器，所有UI的操作，都由此类完成
class UIManager : Singleton<UIManager>
{
    GameObject _root;

    // 游戏的UI事件系统
    EventSystem _eventSys;

    Dictionary<UILayer, GameObject> _allLayerRoot = new Dictionary<UILayer, GameObject>();

    public bool OpenEventSystem
    {
        get { return _eventSys.enabled; }
        set { _eventSys.enabled = value; }
    }

    // 表示UI的层次结构
    public void Init()
    {
        // 加载UI层次结构预制体
        _root = ResMgr.Instance.GetGameObject("UI/UISystem");
        GameObject.DontDestroyOnLoad(_root);

        _eventSys = _root.FindComponent<EventSystem>("EventSystem");

        // 保存层次根节点
        _allLayerRoot.Add(UILayer.Scene, _root.transform.Find("Scene").gameObject);
        _allLayerRoot.Add(UILayer.Touch, _root.transform.Find("Touch").gameObject);
        _allLayerRoot.Add(UILayer.FightUI, _root.transform.Find("FightUI").gameObject);
        _allLayerRoot.Add(UILayer.Normal, _root.transform.Find("Normal").gameObject);
        _allLayerRoot.Add(UILayer.Top, _root.transform.Find("Top").gameObject);
    }

    // UI的加载(ui的预制体路径，ui所在的层次)
    // 返回新生成的UI根节点
    public GameObject AddUI(string path, UILayer layer = UILayer.Normal)
    {
        var uiRoot = ResMgr.Instance.GetGameObject(path);

        GameObject layerRoot = findLayerRoot(layer);
        if (layerRoot != null)
        {
            uiRoot.transform.SetParent(layerRoot.transform, false);
            return uiRoot;
        }
        else
        {
            Debug.LogError("未找到UI层次节点：" + layer);
            return null;
        }
    }

    // 替换UI，返回新UI的根节点
    public GameObject ReplaceUI(string path, UILayer layer = UILayer.Normal)
    {
        // 删除之前的UI
        var layerRoot = findLayerRoot(layer);
        for (int i =0; i < layerRoot.transform.childCount; ++i)
        {
            Release(layerRoot.transform.GetChild(i).gameObject);
        }

        return AddUI(path, layer);
    }

    // 获取某一层的根节点
    private GameObject findLayerRoot(UILayer layer)
    {
        GameObject layerRoot;
        _allLayerRoot.TryGetValue(layer, out layerRoot);

        return layerRoot;
    }

    // UI的删除
    public void Release(GameObject uiRoot)
    {
        ResMgr.Instance.Release(uiRoot);
    }
}

/// <summary>
/// UI层次
/// </summary>
public enum UILayer
{
    Scene,
    Touch,
    FightUI,
    Normal,
    Top
}
